package 武汉大学;

import com.alibaba.excel.EasyExcel;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import 电子科技大学.DZUBean;

import java.io.IOException;
import java.util.*;

public class WHu {
    public static void main(String[] args) throws IOException {
        String Url = "http://smee.whut.edu.cn/rsgz/szdw/";
        String url = "";
        int i = 0;
        int i2 = 0;
        HashMap<Integer, String> integerStringHashMap = new HashMap<>();
        HashMap<Integer, String> integerStringHashMap_url = new HashMap<>();

        Connection connect = Jsoup.connect(Url);
        Document post = connect.post();
        for (Element element : post.getElementsByTag("table")) {
            for (Element td : element.getElementsByTag("td")) {
                if (td.text().equals("    ") | td.text().equals(" ") | td.text().equals("      ")) {
                    continue;
                } else {
                    integerStringHashMap.put(i++, td.text());
                    if (td.getElementsByTag("a").attr("href").equals("")) {
                        continue;
                    } else {
                        url = td.getElementsByTag("a").attr("href").substring(2, td.getElementsByTag("a").attr("href").length());
                        integerStringHashMap_url.put(i2++, url);
                    }
                }
            }
        }
//        Set<Map.Entry<Integer, String>> ms = integerStringHashMap.entrySet();
//        for (Map.Entry<Integer, String> m : ms) {
//            System.out.println(m.getKey() + "=" + m.getValue());
//        }
//        Set<Map.Entry<Integer, String>> ms2 = integerStringHashMap_url.entrySet();
//        for (Map.Entry<Integer, String> m : ms2) {
//            System.out.println(m.getKey() + "=" + m.getValue());
//        }
        WhuBean whuBean = null;
        List<WhuBean> whuBeans = new ArrayList<>();
        for (int j = 8; j <= 48; j++) {
            whuBean = new WhuBean();
            String s = integerStringHashMap.get(j);
            whuBean.setT_name(s);
//            System.out.println(s);//name
            String s2 = integerStringHashMap_url.get(j - 5);
            String url3 = "http://smee.whut.edu.cn/rsgz/szdw/" + s2;//url
            Connection connect1 = Jsoup.connect(url3);
            Document post1 = connect1.post();
//            System.out.println(post1.getElementsByClass("art_text").text().replaceAll(" +", ""));
            whuBean.setInfo(post1.getElementsByClass("art_text").text().replaceAll(" +", ""));
            whuBeans.add(whuBean);
        }
        EasyExcel.write("src/WHu.xlsx", WhuBean.class).sheet("user").doWrite(whuBeans);
    }
}
